using System;
using System.Collections;
using System.Windows.Forms;

namespace Publish.UserControl
{
    /// <summary>
    /// 自定义 ListView 控件
    /// </summary>
    public class ListViewCustom : ListView
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public ListViewCustom()
        {
            //设置双缓冲
            SetStyle(ControlStyles.DoubleBuffer |
                              ControlStyles.OptimizedDoubleBuffer |
                              ControlStyles.AllPaintingInWmPaint,
                              true);
            UpdateStyles();

            //设置显示方式
            //this.CheckBoxes = true;
            //this.View = View.SmallIcon;
        }

        /// <summary>
        /// ListView排序
        /// </summary>
        /// <param name="lvControl">排序的控件</param>
        /// <param name="columnIndex">按那一列排序的下标</param>
        public void SortListViewItem(ListView lvControl, Int32 columnIndex = 13)//控件、下标 13是ID列
        {
            #region Back Code

            //var asc = ((char)0x25bc).ToString().PadLeft(0, ' ');//修改数字0，数字越大，小箭头越靠右
            //var des = ((char)0x25b2).ToString().PadLeft(0, ' ');//加上小三角

            //if (sort == false)
            //{
            //    sort = true;
            //    string oldStr = lvControl.Columns[e.Column].Text.TrimEnd((char)0x25bc, (char)0x25b2, ' ');
            //    lvControl.Columns[e.Column].Text = oldStr + Des;
            //}
            //else if (sort == true)
            //{
            //    sort = false;
            //    string oldStr = lvControl.Columns[e.Column].Text.TrimEnd((char)0x25bc, (char)0x25b2, ' ');
            //    lvControl.Columns[e.Column].Text = oldStr + Asc;
            //}

            //var oldStr = lvControl.Columns[index].Text.TrimEnd((char)0x25bc, (char)0x25b2);
            //lvControl.Columns[Index].Text = oldStr + Des;

            //int rowCount = lvControl.Items.Count;


            //if (currentCol != -1)
            //{
            //    for (int i = 0; i < rowCount; i++)
            //    {
            //        lvControl.Items[i].UseItemStyleForSubItems = false;
            //        lvControl.Items[i].SubItems[currentCol].BackColor = Color.White;

            //        if (e.Column != currentCol)
            //            lvControl.Columns[currentCol].Text = lvControl.Columns[currentCol].Text.TrimEnd((char)0x25bc, (char)0x25b2, ' ');
            //    }
            //}

            //for (int i = 0; i < rowCount; i++)
            //{
            //    lvControl.Items[i].UseItemStyleForSubItems = false;
            //    lvControl.Items[i].SubItems[e.Column].BackColor = Color.WhiteSmoke;
            //    currentCol = e.Column;
            //}

            #endregion

            lvControl.ListViewItemSorter = new ListViewCustom.ListViewItemComparer(columnIndex, false);
            lvControl.Sort();
        }

        /// <summary>
        /// 自定义 listView 排序规则
        /// </summary>
        public class ListViewItemComparer : IComparer
        {
            private readonly int ColumnIndex = 0;//列的下标
            private readonly bool IsAsc;//排序方式（正序，倒序）

            /// <summary>
            /// 设置排序列，及排序方式（正序，倒序）
            /// </summary>
            /// <param name="columnIndex">需要排序列的下标</param>
            /// <param name="isAsc">true：正序，false倒序</param>
            public ListViewItemComparer(int columnIndex, bool isAsc)
            {
                ColumnIndex = columnIndex;
                IsAsc = isAsc;
            }

            public int Compare(object x, object y)
            {
                var textX = ((ListViewItem)x).SubItems[ColumnIndex].Text;
                var textY = ((ListViewItem)y).SubItems[ColumnIndex].Text;

                return IsAsc ? String.Compare(textX, textY) :
                    String.Compare(textY, textX);
            }
        }
    }

    //public static class ExpandListViewCustom
    //{

    //}
}
